package com.rockcent.api.controller;

import com.rockcent.api.Routes;
import com.rockcent.api.helper.SMSHelper;
import com.rockcent.api.helper.UserHelper;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.Mall;
import com.rockcent.mall.lib.dto.BankCardDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.repository.MallRepository;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hone on 17-4-15.
 */
@RestController
@RequestMapping(value = Routes.API_VERSION)
public class MerchantBankCardController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MerchantBankCardController.class);
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MallRepository mallRepository;
    @Autowired
    private SMSHelper sMSHelper;

    /**
     * @api {get} /v1/merchant/bankcard/send_code 发送手机验证码(银行卡解绑与绑定)
     * @apiGroup B MERCHANT
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {String} mobile 手机号码
     *
     * @apiSuccess {String} success Y为成功
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     *  "data":{
     *      "success": "Y"
     *   }
     * }
     *
     */
    @RequestMapping(value = Routes.MERCHANT_BANKCARD_SEND_CODE, method = RequestMethod.GET)
    public JsonResult sendCode(BankCardDto dto, HttpServletRequest request) {
        UserInfoDto userDto = userHelper.getSession(request);
        Long mallId = userDto.mall.getId();
        RestDoing doing = jsonResult -> {
            if (StringUtils.isEmpty(dto.getMobile())) {
                jsonResult.remind("手机号码不能为空", log);
                return;
            }
            Mall mall = mallRepository.findOne(mallId);
            String mallName = mall.getName();
            sMSHelper.sendCardCode(dto.getMobile(), mallName, 60);
            String ret = "{\"success\":\"Y\"}";
            jsonResult.data = JSONObject.fromObject(ret);
        };
        return doing.go(request, log);
    }

    /**
     * @api {get} /v1/merchant/bankcard/check_code 检查手机验证码是否有效(银行卡解绑与绑定)
     * @apiGroup B MERCHANT
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {String} mobile 手机号码
     * @apiParam {String} code 验证码
     *
     * @apiSuccess {String} success Y为成功
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     *  "data":{
     *      "success": "Y"
     *   }
     * }
     *
     */
    @RequestMapping(value = Routes.MERCHANT_BANKCARD_CHECK_CODE, method = RequestMethod.GET)
    public JsonResult checkCode(BankCardDto dto, HttpServletRequest request) {
        RestDoing doing = jsonResult -> {
            if(StringUtils.isEmpty(dto.getMobile())) {
                jsonResult.remind("手机号码不能为空", log);
                return;
            }
            if(StringUtils.isEmpty(dto.getCode())) {
                jsonResult.remind("验证码不能为空", log);
                return;
            }
            boolean isOk = sMSHelper.checkCodeEffective(dto.getMobile(),dto.getCode());
            String ret = "{\"success\":\"N\"}";
            if(isOk) {
                ret = "{\"success\":\"Y\"}";
            }
            jsonResult.data = JSONObject.fromObject(ret);
        };
        return doing.go(request, log);
    }

}
